/*
Copyright (c) 2011, Jeffrey Kunkel
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:

Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.
Neither the name of the Jeffrey Kunkel nor the names of its contributors 
may be used to endorse or promote products derived from this software 
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LISTENER_H_
#define LISTENER_H_

#include <boost/function.hpp>
#include <vector>

#if _MBCS
# pragma warning(disable:4005)
#endif

#define DATA_PARAMETERS 
#define COMMA_PARAMETERS 
#define FUNCTION boost::function0<void>

template< typename alloc = std::allocator<FUNCTION> >\
class Listener0 {
private:
  std::vector< FUNCTION, alloc > listens;
  typedef typename std::vector< FUNCTION >::iterator iterator;
public:
  void operator()( DATA_PARAMETERS ) {
    for( iterator itr = listens.begin(), end = listens.end();
    itr != end; ++itr ) 
      (*itr)( COMMA_PARAMETERS );
  }
  void addListener( FUNCTION f ) {
    listens.push_back(f);
  }
  void removeAllListeners() {
    listens.clear();
  }
  template< typename Predicate >
  void removeListenerIf(Predicate p) {
    iterator itr = std::remove_if( listens.begin(), listens.end(), p );
    listens.resize( abs(listens.end() - itr) );
  }
};

#define DATA_PARAMETERS P0 p0
#define COMMA_PARAMETERS p0
#define TEMPLATE_PARAMETERS typename P0
#define FUNCTION boost::function1<void, P0>

#define CLASS(x) \
  template< TEMPLATE_PARAMETERS, typename alloc = std::allocator<FUNCTION> >\
  class Listener##x {\
  private:\
    std::vector< FUNCTION, alloc > listens;\
    typedef typename std::vector< FUNCTION >::iterator iterator;\
  public:\
    void operator()( DATA_PARAMETERS ) {\
      for( iterator itr = listens.begin(), end = listens.end();\
      itr != end; ++itr ) \
        (*itr)( COMMA_PARAMETERS );\
    }\
    void addListener( FUNCTION f ) {\
      listens.push_back(f);\
    }\
    void removeAllListeners() {\
      listens.clear();\
    }\
    template< typename Predicate >\
    void removeListenerIf(Predicate p) {\
      iterator itr = std::remove_if( listens.begin(), listens.end(), p );\
      listens.resize( abs(listens.end() - itr) );\
    }\
  };

#define DATA_PARAMETERS P0 p0
#define COMMA_PARAMETERS p0
#define TEMPLATE_PARAMETERS typename P0
#define FUNCTION boost::function1<void, P0>
CLASS(1)
#define DATA_PARAMETERS P0 p0, P1 p1
#define COMMA_PARAMETERS p0, p1
#define TEMPLATE_PARAMETERS typename P0, typename P1
#define FUNCTION boost::function2<void, P0, P1>
CLASS(2)
#define DATA_PARAMETERS P0 p0, P1 p1, P2 p2
#define COMMA_PARAMETERS p0, p1, p2
#define TEMPLATE_PARAMETERS typename P0, typename P1, typename P2
#define FUNCTION boost::function3<void, P0, P1, P2>
CLASS(3)
#define DATA_PARAMETERS P0 p0, P1 p1, P2 p2, P3 p3
#define COMMA_PARAMETERS p0, p1, p2, p3
#define TEMPLATE_PARAMETERS typename P0, typename P1, typename P2, typename P3
#define FUNCTION boost::function4<void, P0, P1, P2, P3>
CLASS(4)
#define DATA_PARAMETERS P0 p0, P1 p1, P2 p2, P3 p3, P4 p4
#define COMMA_PARAMETERS p0, p1, p2, p3, p4
#define TEMPLATE_PARAMETERS typename P0, typename P1, typename P2, typename P3, typename P4
#define FUNCTION boost::function5<void, P0, P1, P2, P3, P4>
CLASS(5)
#define DATA_PARAMETERS P0 p0, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5
#define COMMA_PARAMETERS p0, p1, p2, p3, p4, p5
#define TEMPLATE_PARAMETERS typename P0, typename P1, typename P2, typename P3, typename P4, typename P5
#define FUNCTION boost::function6<void, P0, P1, P2, P3, P4, P5>
CLASS(6)
#define DATA_PARAMETERS P0 p0, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6
#define COMMA_PARAMETERS p0, p1, p2, p3, p4, p5, p6
#define TEMPLATE_PARAMETERS typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6
#define FUNCTION boost::function7<void, P0, P1, P2, P3, P4, P5, P6>
CLASS(7)
#define DATA_PARAMETERS P0 p0, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7
#define COMMA_PARAMETERS p0, p1, p2, p3, p4, p5, p6, p7
#define TEMPLATE_PARAMETERS typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7
#define FUNCTION boost::function8<void, P0, P1, P2, P3, P4, P5, P6, P7>
CLASS(8)
#define DATA_PARAMETERS P0 p0, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8
#define COMMA_PARAMETERS p0, p1, p2, p3, p4, p5, p6, p7, p8
#define TEMPLATE_PARAMETERS typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8
#define FUNCTION boost::function9<void, P0, P1, P2, P3, P4, P5, P6, P7, P8>
CLASS(9)
#define DATA_PARAMETERS P0 p0, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9
#define COMMA_PARAMETERS p0, p1, p2, p3, p4, p5, p6, p7, p8, p9
#define TEMPLATE_PARAMETERS typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9
#define FUNCTION boost::function10<void, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9>
CLASS(10)

#undef DATA_PARAMETERS
#undef COMMA_PARAMETERS
#undef TEMPLATE_PARAMETERS
#undef FUNCTION
#undef CLASS

#endif
